//引入接口
import API from '../../config/server.js';
// const API = require('../../config/server.js');
Page({
  data:{
    nav:[],
    heroList:[],
    allList:[],
    navType: 'all'
  },
  onLoad()
  {
    //生命周期钩子
    this.heroData()
  },

  /**
   * 请求数据（英雄分类、列表）
   */
  heroData(){
    let that = this;
    console.log(API.herolist);
    wx.showLoading({
      title: '加载中',
    })
    wx.request({  //异步
      url: API.herolist,
      method: 'GET',
      dataType: 'json',
      success: function (res) {
        let data = res.data.data;
        that.setData({
          nav: data.nav,
          heroList: data.heroList,
          allList: data.heroList
        })
      },
      fail: function (res) { 
        wx.showToast({
          title: '网络错误',
          icon: 'error'
        })
      },
      complete: function (res) { 
        wx.hideLoading()  //关闭loading
      },
    })
  },
  changeNav(e){
    let type = e.currentTarget.dataset.type;

    let newList = [];
    let list = this.data.allList;

    if(type=='all'){
      newList = list;
    }else{
      list.filter(function(hero){
        // console.log(hero)
        if(hero.camptype == type){
          newList.push(hero)
        }
      })
     
    }
    this.setData({
      navType: type,
      heroList: newList
    })
  },
  toDetail(e){
    //接收两个属性值
    let info_id = e.currentTarget.dataset.id;
    let info_url = e.currentTarget.dataset.url;
    wx.navigateTo({
      url: `/pages/heroDetail/heroDetail?id=${info_id}&url=${info_url}`,
    })
  }
})